package com.lc.recovery.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.recovery.R;
import com.lc.recovery.app.AppActivity;
import com.lc.recovery.http.request.RegisterApi;
import com.lc.recovery.http.response.RegisterBean;
import com.lc.recovery.manager.ActivityManager;
import com.lc.recovery.other.IntentKey;
import com.lc.recovery.util.SPUtil;
import com.lc.recovery.util.constant.EvenConstant;
import com.lc.recovery.util.eventbus.Event;
import com.lc.recovery.util.eventbus.EventBusUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PerfectInformationActivity extends AppActivity {
    @BindView(R.id.tv_1)
    TextView tv1;
    @BindView(R.id.et_perfect_name)
    EditText etPerfectName;
    @BindView(R.id.tv_2)
    TextView tv2;
    @BindView(R.id.et_perfect_phone)
    EditText etPerfectPhone;
    @BindView(R.id.tv_3)
    TextView tv3;
    @BindView(R.id.et_perfect_address_check)
    TextView etPerfectAddressCheck;
    @BindView(R.id.ll_address_check)
    LinearLayout llAddressCheck;
    @BindView(R.id.tv_4)
    TextView tv4;
    @BindView(R.id.et_perfect_address)
    EditText etPerfectAddress;
    private String type;
    private String mobile;
    private String password;
    private String code;
    private String share_code;
    private String typenum;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_perfectinformation;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        mobile = getIntent().getStringExtra("mobile");
        password = getIntent().getStringExtra("password");
        code = getIntent().getStringExtra("code");
        share_code = getIntent().getStringExtra("share_code");
        type = getIntent().getStringExtra("type");
        if (type.equals("商户")){
            tv1.setText("商家名称");
            tv2.setText("联系电话");
            tv3.setText("商户位置");
            tv4.setText("详细位置");
            etPerfectName.setHint("请输入您的商户名称");
            etPerfectPhone.setHint("请输入您的联系电话");
            etPerfectAddressCheck.setText("请选择您的所在城市");
            etPerfectAddress.setHint("请输入您的详细地址");
        }else {
            tv1.setText("您的姓名");
            tv2.setText("联系电话");
            tv4.setText("社区名称");
            llAddressCheck.setVisibility(View.GONE);
            etPerfectName.setHint("请输入您的姓名");
            etPerfectPhone.setHint("请输入您的联系电话");
            etPerfectAddress.setHint("请输入您的社区名称");
        }
    }

    @OnClick({R.id.ll_address_check, R.id.tv_to_register})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_address_check:
                break;
            case R.id.tv_to_register:
                register();
                break;
        }
    }

    private void register() {
        if (etPerfectAddress.getText().toString().isEmpty()){
            toast("地址不能为空");
            return;
        }
        if (type.equals("商户")){
            if (etPerfectName.getText().toString().isEmpty()){
                toast("商户名称不能为空");
                return;
            }

            typenum  = "1";
        }else {
            if (etPerfectName.getText().toString().isEmpty()){
                toast("社区名称不能为空");
                return;
            }
            typenum  = "2";
        }
        EasyHttp.post(this)
                .api(new RegisterApi()
                        .setMobile(mobile)
                        .setPassword(password)
                        .setCode(code)
                        .setShare_code(share_code)
                        .setType(typenum)
                        .setReal_name(etPerfectName.getText().toString())
                        .setAddress(etPerfectAddress.getText().toString()))
                .request(new HttpCallback<RegisterBean>(this) {



                    @Override
                    public void onSucceed(RegisterBean data) {
                        if (data.getCode()==200){
                            if (data.getCode()==200){
                                SPUtil.put(PerfectInformationActivity.this, IntentKey.USERID,data.getUser_id());
                                SPUtil.put(PerfectInformationActivity.this, IntentKey.TYPE,typenum+"");
//                                     btnLoginCommit.showSucceed();
                                // 进行内存优化，销毁除登录页之外的所有界面
                                ActivityManager.getInstance().finishAllActivities();
                                EventBusUtils.sendEvent(new Event(EvenConstant.TYPE, typenum));
                                startActivity(HomeActivity.class);

                            }else {
                                toast(data.getMsg());

                            }
                            finish();
                        }else {
                            toast(data.getMsg());

                        }


                    }

                    @Override
                    public void onFail(Exception e) {
//                        super.onFail(e);

                    }
                });
    }
}
